package com.cmb.constant;

import com.alibaba.excel.util.StringUtils;
import com.cmb.dto.TableColumnInfo;

import java.math.BigDecimal;

public class JavaFileConstant {

    public static final String DOCS_START = "/**";

    public static final String DOCS_MIDDLE = " *%s";

    public static final String DOCS_END = "**/";

    public static final String FIRST_ROW = "public void %s {";

    public static final String LAST_ROW = "}";

    public static final String STRING_ROW = "private String %s;";

    public static final String NUMBER_ROW = "private BigDecimal %s;";

    public static final String ZZS001 = "ZZS001";

    public static String createNotBlank() {
        return "@NotBlank(message = \"ZZS001\")";
    }

    public static String createNumberAnnotation(String numberFormat) {
        String substring = numberFormat.substring(1);
        String stringArray = substring.substring(0, substring.length() - 1);
        String[] numberArray = stringArray.split(",");
        return String.format("@Digits(integer=%s,fraction = %s,message = \"整数位上限为%s位，小数位上限为%s位\")",
                numberArray[0],
                numberArray[1],
                numberArray[0],
                numberArray[1]);
    }

    public static String createLengthAnnotation(int length, Boolean consZh) {
        String checkType = consZh ? "StringLength.CheckType.CHECK_STRING_LENGTH" : "StringLength.CheckType.CHECK_BYTE_LENGTH";
        return String.format("@StringLength(max = %d,checkType = %s, message = \"字段长度最大不能超过{max}位\")", length, checkType);
    }

    public static String createColumnDesc(String desc) {
        String oneRowDesc = DOCS_START + "\n" + DOCS_MIDDLE + "\n" + DOCS_END + "\n";
        return String.format(oneRowDesc, desc);
    }

    public static String createNotes(String desc) {
        return String.format(DOCS_MIDDLE, desc);
    }

    public static String createFirstRow(String clazzName) {
        return String.format(FIRST_ROW, clazzName);
    }

    public static String createStringProperty(String propertyName) {
        return String.format(STRING_ROW, propertyName);
    }

    public static String createNumberProperty(String propertyName) {
        return String.format(NUMBER_ROW, propertyName);
    }

    public static String createSingleColumnSql(TableColumnInfo tableColumnInfo) {
        // 还没有在关键字之间添加空格
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(tableColumnInfo.getEnColumnName() + " ")
                .append(tableColumnInfo.getColumnType() + " ")
                .append("(")
                .append(tableColumnInfo.getColumnLength())
                .append(") ");
        if (!StringUtils.isBlank(tableColumnInfo.getDefaultValue())) {
            stringBuilder.append("DEFULT" + " " + tableColumnInfo.getDefaultValue().replaceAll("\"","\'") + " ");
        }

        if ("Y".equals(tableColumnInfo.getSpaceFlag())) {
            stringBuilder.append("NOT NULL");
        }
        stringBuilder.append("," + System.lineSeparator());
        return stringBuilder.toString();
    }
}
